package LandR.register.phone.phoneSrc.function;

import java.sql.*;

public class registerHelpFunction {
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        final String URL = "jdbc:mysql://123.57.3.195:3306/YDYP?characterEncoding=utf8&useSSL=true&serverTimezone=UTC";
        final String username = "root";
        final String password = "ZAMTYR1314";
        Connection conn = DriverManager.getConnection(URL, "root", "ZAMTYR1314");
        if (conn == null) {
            System.out.println("创建连接失败");
            close(conn);
            return null;
        } else {
            System.out.println("创建连接成功");
            return conn;
        }

    }

    //2.getPrepareStatement  获取PrepareStatement接口方法
    public static PreparedStatement getPrepareStatement(String sql) throws SQLException, ClassNotFoundException {
        Connection conn = getConnection();
        PreparedStatement ppsm = (PreparedStatement) conn.prepareStatement(sql);
        return ppsm;
    }

    //3.关闭prepareStatement接口的方法
    public static void close(PreparedStatement pstmt) {
        if (pstmt != null) {                        //避免出现空指针异常
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

    //4.关闭connection接口的方法
    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }

    //5.关闭resultSet接口的方法
    public static void close(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }

    //6.将登录者的手机号存入数据库
    public void insertUserInUsers(User user) throws SQLException, ClassNotFoundException {
        String sql = "insert into users (phonenumber,password) values (?,?)";
        Connection conn = null;
        PreparedStatement ppst = null;

        String phoneNumber = user.getPhoneNumber();
        String password = user.getPassword();

        try {
            conn = getConnection();
            ppst = getPrepareStatement(sql);
            ppst.setString(1, phoneNumber);
            ppst.setString(2, password);
            ppst.executeUpdate();
            System.out.println("手机注册用户创建成功,手机号上传成功");
        } finally {
            ppst.close();
            conn.close();
        }
    }

    //7.根据手机号从数据库中返回用户的id
    public User selectUserFromUsers(User user) throws SQLException {
        String sql = "select * from users where phonenumber = ?";
        Connection conn = null;
        PreparedStatement ppst = null;
        ResultSet rs = null;
        String phoneNumber = user.getPhoneNumber();
        try {
            conn = getConnection();
            ppst = getPrepareStatement(sql);
            ppst.setString(1, phoneNumber);
            rs = (ResultSet) ppst.executeQuery();
            while (rs.next()) {
                Integer id = rs.getInt("id");
                user.setId(id);
            }

            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            //添加关闭
            rs.close();
            ppst.close();
            conn.close();
        }
        return user;
    }

    public boolean judegeUserFromUsersByPhoneNumber(User user) throws SQLException {
        String sql = "select * from users where phonenumber = ?";
        Connection conn = null;
        PreparedStatement ppst = null;
        ResultSet rs = null;
        String phoneNumber = user.getPhoneNumber();
        try {
            conn = getConnection();
            ppst = getPrepareStatement(sql);
            ppst.setString(1, phoneNumber);
            rs = (ResultSet) ppst.executeQuery();
            if (rs.next()) {
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            //添加关闭
            rs.close();
            ppst.close();
            conn.close();
        }
        return false;
    }


}
